package com.higgins.eventtracking.client.health;

import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.DietTrackingService;
import com.higgins.eventtracking.client.DietTrackingServiceAsync;
import com.higgins.eventtracking.client.dto.InventoryDTO;
import com.higgins.eventtracking.client.dto.MealDTO;
import com.higgins.eventtracking.client.health.components.GenericChartPanel;
import com.higgins.eventtracking.client.health.components.GenericTabInterface;

public class InventoryTab extends VerticalPanel		implements GenericTabInterface	{
		
	final Label textToServerLabel = new Label();
	final HTML serverResponseLabel = new HTML();
	final DialogBox dialogBox = new DialogBox();
	final Button closeButton = new Button("Close");
	final Label errorLabel = new Label();
	
	private TextBox nameBox = new TextBox();
	private TextBox sellByDateBox = new TextBox();
	private CheckBox breakfast = new CheckBox();
	private CheckBox main = new CheckBox();
	private CheckBox snack = new CheckBox();
	private Button addButton = new Button("Add");

	private final DietTrackingServiceAsync dietTrackingService = GWT
	.create(DietTrackingService.class);
	private boolean firstRun = true;
	private Integer tabIndex = null;
	private String tabTitle = null;
	
	@Override
	public Integer getTabIndex() {
		return tabIndex;
	}
	@Override
	public void setTabIndex(Integer index) {
		this.tabIndex = index;
	}
	@Override
	public String getTabTitle()	{
		return tabTitle;
	}
	@Override
	public void setTabTitle(String tabTitle)	{
		this.tabTitle = tabTitle;
	}

	public InventoryTab()	{
	}
	public void update()	{
		if (firstRun)
			initialise();
	}
	public void initialise()	{
		addDialogBox();
		addFields();
		firstRun = false;
	}
	
	private void addFields()	{
		FlowPanel flowPanel = new FlowPanel();
		VerticalPanel namePanel = new VerticalPanel();
		namePanel.add(new Label("Name:"));
		namePanel.add(nameBox);
		flowPanel.add(namePanel);

		VerticalPanel sellByDatePanel = new VerticalPanel();
		sellByDatePanel.add(new Label("SellByDate:"));
		sellByDatePanel.add(sellByDateBox);
		flowPanel.add(sellByDatePanel);
		
		VerticalPanel breakfastPanel = new VerticalPanel();
		breakfast.setText("Breakfast: ");
		breakfastPanel.add(breakfast);
		flowPanel.add(breakfastPanel);
		
		VerticalPanel mainPanel = new VerticalPanel();
		main.setText("Main: ");
		mainPanel.add(main);
		flowPanel.add(mainPanel);

		VerticalPanel snackPanel = new VerticalPanel();
		snack.setText("Snack: ");
		snackPanel.add(snack);
		flowPanel.add(snackPanel);

		VerticalPanel addButtonPanel = new VerticalPanel();
		addButtonPanel.add(addButton);
		flowPanel.add(addButtonPanel);

		addButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				InventoryDTO dto = new InventoryDTO();
				dto.setName(nameBox.getText());
				dto.setSellByDateString(sellByDateBox.getText());
				dto.setBreakfast(breakfast.getValue());
				dto.setMain(main.getValue());
				dto.setSnack(snack.getValue());
				insertInventory(dto);
			} });

		
		add(flowPanel);		
	}
	private void insertInventory(InventoryDTO dto)	{
		dietTrackingService.insertInventory(dto, new AsyncCallback<Boolean>() {
					public void onFailure(Throwable caught) {
					}

					public void onSuccess(Boolean result) {
						nameBox.setText("");
						sellByDateBox.setText("");
						breakfast.setValue(false);
						main.setValue(false);
						snack.setValue(false);
					}
				});
	}
	
	private void addDialogBox()	{
		dialogBox.setText("Remote Procedure Call");
		dialogBox.setAnimationEnabled(true);
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);

		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});		
	}
	@Override
	public void addChartPanel(GenericChartPanel chartPanel) {
		// TODO Auto-generated method stub
		
	}
}
